//package com.wpp.event;
//
//import com.alibaba.druid.pool.DruidDataSource;
//import com.wpp.Config.DynamicDataSource;
//import com.wpp.config.ZkConfigCenter;
//import com.wpp.constant.Constant;
//import com.wpp.util.SpringContentHolder;
//import org.apache.curator.framework.CuratorFramework;
//import org.apache.curator.framework.recipes.cache.PathChildrenCache;
//import org.apache.curator.framework.recipes.cache.PathChildrenCacheEvent;
//import org.apache.curator.framework.recipes.cache.PathChildrenCacheListener;
//import org.springframework.boot.ApplicationArguments;
//import org.springframework.boot.ApplicationRunner;
//
//import javax.sql.DataSource;
//import java.util.Properties;
//import java.util.concurrent.atomic.AtomicReference;
//
///**
// * @Classname DataSourceEvent
// * @Description TODO
// * @Date 2020/12/11 22:36
// * @Created by wpp25
// */
//public class DataSourceEvent implements ConfigEvent, ApplicationRunner {
//
//
//    @Override
//    public void execute() {
//        refreshDataSource();
//    }
//
//    @Override
//    public void run(ApplicationArguments args) throws Exception {
//        refreshDataSource();
//    }
//
//    /**
//     * 刷新数据库配置连接
//     */
//    private void refreshDataSource() {
//        ZkConfigCenter zkConfigCenter = SpringContentHolder.getBean(ZkConfigCenter.class);
//
//        PathChildrenCache pathChildrenCache = new PathChildrenCache(zkConfigCenter.getCuratorFramework(), Constant.ZkConfig.CONFIG_ROOT, true);
//        PathChildrenCacheListener pathChildrenCacheListener = (curatorFramework,event) -> {
//            // 更新节点
//            if (event.getType() == PathChildrenCacheEvent.Type.CHILD_UPDATED) {
//                DruidDataSource druidDataSource = new DruidDataSource();
//
//                Properties properties = zkConfigCenter.loadConfig();
//
//                druidDataSource.setDriverClassName(properties.getProperty("spring.datasource.driver-class-name"));
//                druidDataSource.setUrl(properties.getProperty("spring.datasource.url"));
//                druidDataSource.setUsername(properties.getProperty("spring.datasource.username"));
//                druidDataSource.setPassword(properties.getProperty("spring.datasource.password"));
//
//
//                DynamicDataSource bean = SpringContentHolder.getBean(DynamicDataSource.class);
//                AtomicReference<DataSource> referenceDataSource = bean.getReferenceDataSource();
//                referenceDataSource.getAndSet(druidDataSource);
//                System.out.println("DataSourceEvent 重置数据源成功");
//            }
//
//        };
//        pathChildrenCache.getListenable().addListener(pathChildrenCacheListener);
//        try {
//            pathChildrenCache.start();
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//
//    }
//}
